/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package de.fuberlin.kosmik.data;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import de.fuberlin.kosmik.log.Constants;

/**
 * A helper class to manage database creation and version management.
 * 
 */
public class DataOpenHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = DBNames.DATABASE_NAME;
	private static final int DATABASE_VERSION = DBNames.DATABASE_VERSION;

	/**
	 * Constructor for DataOpenHelper.
	 * 
	 * @param context - the context in which this SQLiteOpenHelper is
	 *            instantiated
	 */
	public DataOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// public SQLiteOpenHelper (Context context, String name,
		// SQLiteDatabase.CursorFactory factory, int version)
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		try {
			// String schema = createSchemaString();
			Log.d(Constants.TAG, "creating database");
			// Log.d(Constants.TAG, schema);
			// db.execSQL(schema);

			for (DataBaseQuery bdq : DataBaseQuery.values()) {
				Log.d(Constants.TAG, "creating table with Query: " + bdq.getQuery());
				db.execSQL(bdq.getQuery());
			}

		} catch (SQLException ioe) {
			Log.e(Constants.TAG, ioe.getMessage());
			ioe.printStackTrace();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
		// TODO: Implement
		Log.w(Constants.TAG, "DataOpenHelper.onUpgrade() not yet implemented!");
	}
}
